package com.shopowner.dao.impl;

import com.shopowner.entity.Coach;
import com.shopowner.mapper.CoachMapper;
import com.shopowner.page.PageInfo;
import com.shopowner.utils.GenNumTools;
import com.shopowner.dao.CoachDao;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("coachDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class CoachDaoImpl implements CoachDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private CoachMapper coachMapper;
    public List<Coach>  getAllCoachs() throws Exception{
        return coachMapper.selectByExample(null);
    }

    @Override
    public Integer deleteCoachByCno(String cno)  throws Exception{
        return coachMapper.deleteByPrimaryKey(cno);
    }

    @Override
    public Integer updateCoachByCno(Coach coach)  throws Exception{
        return coachMapper.updateByPrimaryKey(coach);
    }

    @Override
    public Integer addCoach(Coach coach)  throws Exception{
        coach.setCno(initId());
        return coachMapper.insertSelective(coach);
    }
    public String initId(){
        String maxCno = coachMapper.getMaxCno();
        return GenNumTools.initId("c",4,maxCno);
    }

    @Override
    public List<Coach> getCoachByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)coachMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return coachMapper.getCoachByPage(map);
    }

    @Override
    public Integer updateCoachs(Coach coach) {
        return null;
    }
//
//    @Override
//    public Coach selectCoachWithChildren(String cno){
//        return coachMapper.selectCoachWithChildren(cno);
//    }
//
//    @Override
//    public Coach getCoachWithOrdrees(String cno) throws Exception {
//        return coachMapper.getCoachWithOrdrees(cno);
//    }
//
//    @Override
//    public Coach getAccountByAccountName(String cname) {
//            return coachMapper.getAccountByAccountName(cname);
//    }
//
//    /**
//     *
//     * @param cname
//     * @return
//     */
//    public List<Coach> getCoachByExample(String cname) {
//        CoachExample coachExample =new CoachExample();
//        coachExample.createCriteria().andCnameEqualTo(cname);
//        return coachMapper.selectByExample(coachExample);
//    }
//
//    @Override
//    public Integer updateCoach(Coach coach) { return coachMapper.updateByPrimaryKey(coach); }

//    public Integer updateByPrimaryKeySelective(Coach coach) { return coachMapper.updateByPrimaryKeySelective(coach); }
//
//
//    @Override
//    public Integer updateCoachs(Coach coach) { return coachMapper.updateByPrimaryKeys(coach); }

}
